/**
 * @(#)BindService.java   2012-10-22
 * 
 * CopyRight (c) 2012-2020 TCL Digital Technology (shenzhen) CO.,Ltd. All rights reserved.
 * 
 * @description: 
 * 
 * @version: 1.0
 * 
 * @modification history:
 *     2012-10-22 Modified By zhaodm : Create
 * 
 * 
 */
package com.zhaodm.practise.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * @description: 
 * 
 */
public class BindService extends Service {

	private int count;
	private boolean quit;
	private MyBinder binder = new MyBinder();
	
	public class MyBinder extends Binder {
		public int getCount() {
			return count;
		}
	}
	/* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "Service is binded!!");
		return binder;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Service#onCreate()
	 */
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "Service is created!");
		new Thread() {
			/* (non-Javadoc)
			 * @see java.lang.Thread#run()
			 */
			public void run() {
				while(!quit) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count++;
				}
			}
		}.start();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Service#onUnbind(android.content.Intent)
	 */
	public boolean onUnbind(Intent intent) {
		Log.i(TAG, "Service is unbinded!!");
		return true;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Service#onDestroy()
	 */
	public void onDestroy() {
		super.onDestroy();
		this.quit = true;
		Log.i(TAG, "Service is destroyed");
	}

	
	private final String TAG = "BindService";
}
